package com.woniu51.k15demo.controller;

import com.github.pagehelper.PageInfo;
import com.woniu51.k15demo.entity.Manager;
import com.woniu51.k15demo.entity.Permission;
import com.woniu51.k15demo.entity.Role;
import com.woniu51.k15demo.result.CommonResult;
import com.woniu51.k15demo.service.ManagerService;
import com.woniu51.k15demo.vo.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 杨光
 * @date 2022年02月14日 17:44
 */
@RestController
@CrossOrigin
@Api(tags = "权限管理")
public class ManagerController {


    @Autowired
    ManagerService managerService;

    @PostMapping("/manager/login")
    @ApiOperation(value = "登录",notes = "登录")
    public CommonResult<String> doLogin(@RequestBody @ApiParam("登录信息对象") LoginDto loginDto){
        String jwt = managerService.doLogin(loginDto);

        return new CommonResult<>(200,"登录成功",jwt);

    }

    @GetMapping("/manager/queryPermissionWithPage")
    @ApiOperation(value = "分页查询权限列表",notes = "分页查询权限列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum",value = "当前页"),
                       @ApiImplicitParam(name = "pageSize",value = "分页大小"),
                        @ApiImplicitParam(name = "name",value = "权限名称")})
    public  CommonResult<PageInfo<Permission>> queryPermissionWithPage(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            @RequestParam(value = "name",defaultValue = "") String name){
        PageInfo<Permission> result = managerService.queryPermissionWithPage(pageNum,pageSize,name);
        return new CommonResult<>(200,"操作成功",result);
    }

    @GetMapping("/manager/queryFirstLevelPermission")
    public  CommonResult<List<Permission>> queryFirstLevelPermission(){
        List<Permission> result = managerService.queryFirstLevelPermission();
        return new CommonResult<>(200,"操作成功",result);
    }

    @PostMapping("/manager/addPermission")
    public  CommonResult<Void> addPermission(@RequestBody PermissionDto permissionDto){

        managerService.addPermission(permissionDto);
        return new CommonResult<>(200,"操作成功",null);
    }
    @GetMapping("/manager/queryAllRoles")
    public  CommonResult<List<Role>> queryAllRoles(){
        List<Role> result = managerService.queryAllRoles();
        return new CommonResult<>(200,"操作成功",result);
    }

    @GetMapping("/manager/queryRoleWithPage")
    public  CommonResult<PageInfo<Role>> queryRoleWithPage(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            @RequestParam(value = "name",defaultValue = "") String name){
        PageInfo<Role> result = managerService.queryRoleWithPage(pageNum,pageSize,name);
        return new CommonResult<>(200,"操作成功",result);
    }


    @PostMapping("/manager/addRole")
    public  CommonResult<Void> addRole(@RequestBody RoleDto roleDto){

        managerService.addRole(roleDto);
        return new CommonResult<>(200,"操作成功",null);
    }
    @PostMapping("/manager/editRole")
    public  CommonResult<Void> editRole(@RequestBody RoleDto roleDto){

        managerService.editRole(roleDto);
        return new CommonResult<>(200,"操作成功",null);
    }

    @GetMapping("/manager/queryManagerById")
    public  CommonResult<Manager> queryManagerById(Long id){
        Manager result = managerService.queryManagerById(id);
        return new CommonResult<>(200,"操作成功",result);
    }

    @GetMapping("/manager/queryRoleIdListByManagerId")
    public  CommonResult<List<Long>> queryRoleIdListByManagerId(Long id){
        List<Long> result = managerService.queryRoleIdListByManagerId(id);
        return new CommonResult<>(200,"操作成功",result);
    }


    @GetMapping("/manager/getRoleAndPermissionsById")
    public  CommonResult<RoleVo> getRoleAndPermissionsById(Long id){
        RoleVo result = managerService.getRoleAndPermissionsById(id);
        return new CommonResult<>(200,"操作成功",result);
    }

    @GetMapping("/manager/getRoleById")
    public  CommonResult<Role> getRoleById(Long id){
        Role result = managerService.getRoleById(id);
        return new CommonResult<>(200,"操作成功",result);
    }
    @PostMapping("/manager/deleteByRoleId")
    public  CommonResult<Void> deleteByRoleId(Long id){
        managerService.deleteByRoleId(id);
        return new CommonResult<>(200,"操作成功",null);
    }

    //给角色关联权限
    //permString 权限id列表，用字符串分隔  3,5,11
    @PostMapping("/manager/role/permission")
    public CommonResult<Void> addRolePermission(Long roleId,String permString){

        System.out.println("roleId:::"+roleId);
        System.out.println("permString:::"+permString);

        /**
         *
         * 1.把permString字符串转成数组
         * 2.通过流的形式遍历这个字符串数组，并且把每一个元素转成了长整型
         * 3.把这些长整型的数据放入了一个List中
         */

        List<Long> permIdList = Arrays.stream(permString.split(","))
                .map(s -> {
            return Long.valueOf(s);
        }).collect(Collectors.toList());

        System.out.println("permIdList:::"+permIdList);

        return  new CommonResult<>(200,"操作成功",null);
    }
    @GetMapping("/manager/queryPermissionTreeLevel1")
    public  CommonResult<List<PermissionTree>> queryPermissionTreeLevel1(){
        List<PermissionTree> result = managerService.queryPermissionTreeLevel1();
        System.out.println(result);
        return new CommonResult<>(200,"操作成功",result);
    }

    @GetMapping("/manager/queryManagerWithPage")
    public  CommonResult<PageInfo<ManagerVo>> queryManagerWithPage(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            @RequestParam(value = "userName",defaultValue = "") String userName){
        PageInfo<ManagerVo> result = managerService.queryManagerWithPage(pageNum,pageSize,userName);
        return new CommonResult<>(200,"操作成功",result);
    }

    @PostMapping("/manager/editUser")
    public  CommonResult<Void> editUser(@RequestBody ManagerDto managerDto){

        managerService.editUser(managerDto);
        return new CommonResult<>(200,"操作成功",null);
    }


    @GetMapping("manager/queryPermissionsByManagerId")
    public  CommonResult<List<PermissionTree>> queryPermissionsByManagerId(HttpServletRequest request){
        Integer userIdInt= (Integer)request.getAttribute("userId");
        Long userId = Long.valueOf(userIdInt.intValue()+"");
//        Long userId = 50L;
        List<PermissionTree> result = managerService.queryPermissionsByManagerId(userId);
        System.out.println(result);
        return new CommonResult<>(200,"操作成功",result);
    }

}
